/**
 * @file main.c
 * @brief  serial demo
 * 
 * Copyright (C) 2014 JakeYang <add358@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

#include "error_do.h" 
#include "serial.h" 

int main(int argc, const char *argv[])
{
	int serial_fd;
	int len;
        char buf[1024];

	printf("open %s\n",argv[1]);
        serial_fd = SerialOpen(argv[1]);
        if(serial_fd < 0)
                err_ret("SerialOpen failed");

        SerialSetSpeed(serial_fd,115200);
        SerialFlowControl(serial_fd,NO_FLOW_CONTROL);
        SerialSetParam(serial_fd,0,8,1);
        fcntl( serial_fd, F_SETFL, 0);

        while (1) {
                memset(buf, 0, 1024);
                if ((len=read(serial_fd, buf, 1024)) < 0) {
                        err_msg("read failed");
                        break;
                }
                printf("len(%d):%s\n",len, buf);
        }

        close(serial_fd);
	return 0;
}
